using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using WeatherHistory.Api.Configuration;
using WeatherHistory.Data;

var builder = WebApplication.CreateBuilder(args);
builder.ConfigureBuilder();

var app = builder.Build();
app.UpdateEnvironmentVariables();
app.ConfigureMiddlewares();

/* auto migrate database */
var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();

app.UseCors(options =>
    options.AllowAnyHeader()
        .AllowAnyMethod()
        .AllowCredentials()
        .WithOrigins(AppConfiguration.UiUrl));

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.Run();